package com.eorchis.module.sysdeptuser.ui.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.sysdeptuser.domain.SysDepartmentBean;
import com.eorchis.module.sysdeptuser.domain.SysDepartmentUserBean;
import com.eorchis.module.sysdeptuser.domain.SysUserBean;
import com.eorchis.module.sysdeptuser.service.ISysDeptUserService;
import com.eorchis.module.sysdeptuser.ui.commond.SysDeptUserQueryCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: SysDeptUserController<br>
 * Description: <br>
 * Copyright @ 2011~2016 Goldgov .All rights reserved.<br>
 * 
 * @author LongJZ
 * @createDate 2016-1-7
 * @version $Revision: $
 */
@Controller("sysDeptUserController")
@RequestMapping(value = SysDeptUserController.MODULE_PATH)
public class SysDeptUserController {

	public static final String MODULE_PATH="/module/sysdeptuser";
	
	@Resource(name = "com.eorchis.module.sysdeptuser.service.impl.SysDeptUserServiceImpl")
	private ISysDeptUserService sysDeptUserService;

	/**
	 * 同步当前管理范围下的部门人员及关联关系(包含子部门)
	 * @param queryCommond
	 * @author LongJZ
	 * @throws Exception 
	 * @createDate 2016-1-7
	 */
	@RequestMapping(value = "/sysDeptUser")
	public String sysDeptUser(SysDeptUserQueryCommond queryCommond, HttpServletRequest request,HttpServletResponse response,
			  @ModelAttribute("resultState") ResultState resultState) throws Exception {
		if (!PropertyUtil.objectNotEmpty(queryCommond.getSearchTreepath())){
			HttpSession session = request.getSession();
			Department casDept = (Department) session.getAttribute(SessionConstant.CURRENT_SCOPE);
			if (!PropertyUtil.objectNotEmpty(casDept)) {
				resultState.setMessage("管理范围为空!");
				return "";
			}
			queryCommond.setSearchTreepath(casDept.getTreepath());
		}
		String msg = "";
		queryCommond.setQueryType(SysUserBean.QUERY_TYPE);
		if (!sysDeptUserService.sysDeptUser(queryCommond))
			msg = "\n同步人员异常!";
		queryCommond.setQueryType(SysDepartmentBean.QUERY_TYPE);
		if (!sysDeptUserService.sysDeptUser(queryCommond))
			msg += "\n同步部门异常!";
		queryCommond.setQueryType(SysDepartmentUserBean.QUERY_TYPE);
		if (!sysDeptUserService.sysDeptUser(queryCommond))
			msg += "\n同步部门人员关系异常!";
		if (msg.equals(""))
			msg = "同步成功!";
		else
			msg = msg.substring(1);
		resultState.setMessage(msg);
		return "";
	}
}
